<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Author: Jorge Torres
 * Description: Login model class
 */
class Auth_model extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
    public function login(){
        // grab user input
        $username = $this->security->xss_clean($this->input->post('username'));
        $password = $this->security->xss_clean($this->input->post('password'));

        $this->load->library('curl');
        $this->load->helper('webservice');
        $user2 = $this->curl->create(create_web_service_url('user/login/username/'.$username.'/password/'.$password.'/'));
        
        $user = json_decode($this->curl->execute());
        if($user){
	        if($user->status == 'success')
	        {
	            // If there is a user, then create session data
	            $data = array(
	            		'user' => $user,
	                    'validated' => true
	            );
	            $this->session->set_userdata($data);
	            return true;
	        }
        }
         //If the previous process did not validate
        // then return false.
        return false;
    }
    
    public function signup(){
    	// grab user input
    	$user_account_name = $this->security->xss_clean($this->input->post('user_account_name'));
    	$user_password = $this->security->xss_clean($this->input->post('user_password'));
    	$user_name = $this->security->xss_clean($this->input->post('user_name'));
    	$user_surname = $this->security->xss_clean($this->input->post('user_surname'));
    	$user_email = $this->security->xss_clean($this->input->post('user_email'));
    	$user_phone = $this->security->xss_clean($this->input->post('user_phone'));
    	$user_birthdate = $this->security->xss_clean($this->input->post('user_birthdate'));
    	$sex = $this->security->xss_clean($this->input->post('sex'));
    	$role_id = $this->security->xss_clean($this->input->post('role_id'));
    
    	$this->load->library('curl');
    	$post = array('user_account_name'=>$user_account_name,
    			'user_password'=>$user_password,
    			'user_name'=>$user_name,
    			'user_surname'=>$user_surname,
    			'user_email'=>$user_email,
    			'user_phone'=>$user_phone,
    			'user_birthdate'=>date('Y-m-d', strtotime($user_birthdate)),
    			'sex'=>$sex,
    			'role_id'=>$role_id
    	);
    	$this->load->helper('webservice');
    	$this->curl->create(create_web_service_url('user/signup'));
    	$this->curl->post($post);
    	$user = json_decode($this->curl->execute());
    	if($user){
	    	if($user->status == "success")
	    	{
	    		return true;
	    	}
    	}
    	return false;
    }
}
?>